package com.sailor.excel.excelTemplate;


import com.sailor.excel.vo.QuestionVO;
import jxl.Workbook;
import jxl.write.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.util.CollectionUtils;

import java.io.OutputStream;
import java.util.List;


@Slf4j
public class QuestionExcelTemplate {

    public static void getReportExcel(OutputStream responseOutputStream, List<QuestionVO> questionVOList) {
        try {
            //打开文件
            WritableWorkbook book = Workbook.createWorkbook(responseOutputStream);
            //生成名为“第一页”的工作表，参数0表示这是第一页
            WritableSheet sheetOne = book.createSheet("Sheet1", 0);

            sheetOne.getSettings().setDefaultColumnWidth(15);
            WritableFont font = new WritableFont(WritableFont.createFont("微软雅黑"));
            // 单元格定义
            WritableCellFormat cellStyle = new WritableCellFormat();
            // 设置单元格的背景颜色
            cellStyle.setBackground(Colour.WHITE, Pattern.NONE);
            // 设置对齐方式
            cellStyle.setAlignment(Alignment.CENTRE);
            // 设置垂直对齐方式
            cellStyle.setVerticalAlignment(VerticalAlignment.CENTRE);
            cellStyle.setFont(font);
            cellStyle.setWrap(true);

            // 单元格定义
            WritableCellFormat numberStyle = new WritableCellFormat(NumberFormats.THOUSANDS_FLOAT);
            // 设置单元格的背景颜色
            numberStyle.setBackground(Colour.WHITE, Pattern.NONE);
            // 设置对齐方式
            numberStyle.setAlignment(Alignment.CENTRE);
            // 设置垂直对齐方式
            numberStyle.setVerticalAlignment(VerticalAlignment.CENTRE);
            numberStyle.setWrap(true);
            numberStyle.setFont(font);

            // 单元格定义
            WritableCellFormat percentStyle = new WritableCellFormat(NumberFormats.PERCENT_FLOAT);
            // 设置单元格的背景颜色
            percentStyle.setBackground(Colour.WHITE, Pattern.NONE);
            // 设置对齐方式
            percentStyle.setAlignment(Alignment.CENTRE);
            // 设置垂直对齐方式
            percentStyle.setVerticalAlignment(VerticalAlignment.CENTRE);
            percentStyle.setWrap(true);
            percentStyle.setFont(font);


            // 在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
            // 以及单元格内容为test
            int index = 0;
            Label id = new Label(index++, 0, "题号", cellStyle);
            Label content = new Label(index++, 0, "题干", cellStyle);
            Label option = new Label(index++, 0, "选项", cellStyle);
            Label answer = new Label(index++, 0, "答案", cellStyle);
            Label analysis = new Label(index++, 0, "解析", cellStyle);
            Label createTime = new Label(index++, 0, "创建时间", cellStyle);
            Label updateTime = new Label(index++, 0, "更新时间", cellStyle);


            sheetOne.addCell(id);
            sheetOne.addCell(content);
            sheetOne.addCell(option);
            sheetOne.addCell(answer);
            sheetOne.addCell(analysis);
            sheetOne.addCell(createTime);
            sheetOne.addCell(updateTime);


            if (!CollectionUtils.isEmpty(questionVOList)) {

                int row = 0;
                for (QuestionVO vo : questionVOList) {
                    row++;
                    int column = 0;
                    Label idTemp = new Label(column, row, String.valueOf(vo.getId()), cellStyle);
                    Label contentTemp = new Label(++column, row, vo.getContent(), cellStyle);

                    Label optionTemp = new Label(++column, row, vo.getOption(), cellStyle);
                    Label answerTemp = new Label(++column, row, vo.getAnswer(), cellStyle);
                    Label analysisTemp = new Label(++column, row, vo.getAnalysis(), cellStyle);
                    Label createTimeTemp = new Label(++column, row, DateFormatUtils.format(vo.getCreateTime(), "yyyy.MM.dd HH:mm:ss"), cellStyle);


                    sheetOne.addCell(idTemp);
                    sheetOne.addCell(contentTemp);
                    sheetOne.addCell(optionTemp);
                    sheetOne.addCell(answerTemp);
                    sheetOne.addCell(analysisTemp);
                    sheetOne.addCell(createTimeTemp);
                }
            }
            //写入数据并关闭文件
            book.write();
            book.close();
        } catch (Exception e) {
            log.error("generate question report error", e);
        }
    }
}
